#include "crypto_utils.h"

int main() {
    EVP_PKEY *bob_priv = NULL, *alice_pub = NULL;
    DigitalEnvelope envelope = {0};
    unsigned char *plaintext = NULL;
    size_t plaintext_len;
    FILE *file = NULL;
    int ret = 0;

    printf("=== Bob (20231414wsq) - Receiver ===\n\n");

    // 加载Bob的私钥和Alice的公钥
    bob_priv = load_private_key(BOB_PRIVATE_KEY);
    if (!bob_priv) {
        fprintf(stderr, "Failed to load Bob's private key\n");
        return -1;
    }

    alice_pub = load_public_key(ALICE_PUBLIC_KEY);
    if (!alice_pub) {
        fprintf(stderr, "Failed to load Alice's public key\n");
        EVP_PKEY_free(bob_priv);
        return -1;
    }

    // 读取数字信封文件
    file = fopen("envelope.dat", "rb");
    if (!file) {
        fprintf(stderr, "Cannot open envelope.dat\n");
        goto cleanup;
    }

    // 读取密文
    if (fread(&envelope.ciphertext_len, sizeof(size_t), 1, file) != 1) {
        fprintf(stderr, "Failed to read ciphertext length\n");
        fclose(file);
        goto cleanup;
    }

    envelope.ciphertext = malloc(envelope.ciphertext_len);
    if (!envelope.ciphertext) {
        fprintf(stderr, "Memory allocation failed for ciphertext\n");
        fclose(file);
        goto cleanup;
    }

    if (fread(envelope.ciphertext, 1, envelope.ciphertext_len, file) != envelope.ciphertext_len) {
        fprintf(stderr, "Failed to read ciphertext\n");
        fclose(file);
        goto cleanup;
    }

    // 读取加密密钥
    if (fread(&envelope.encrypted_key_len, sizeof(size_t), 1, file) != 1) {
        fprintf(stderr, "Failed to read encrypted key length\n");
        fclose(file);
        goto cleanup;
    }

    envelope.encrypted_key = malloc(envelope.encrypted_key_len);
    if (!envelope.encrypted_key) {
        fprintf(stderr, "Memory allocation failed for encrypted key\n");
        fclose(file);
        goto cleanup;
    }

    if (fread(envelope.encrypted_key, 1, envelope.encrypted_key_len, file) != envelope.encrypted_key_len) {
        fprintf(stderr, "Failed to read encrypted key\n");
        fclose(file);
        goto cleanup;
    }

    // 读取签名
    if (fread(&envelope.signature_len, sizeof(size_t), 1, file) != 1) {
        fprintf(stderr, "Failed to read signature length\n");
        fclose(file);
        goto cleanup;
    }

    envelope.signature = malloc(envelope.signature_len);
    if (!envelope.signature) {
        fprintf(stderr, "Memory allocation failed for signature\n");
        fclose(file);
        goto cleanup;
    }

    if (fread(envelope.signature, 1, envelope.signature_len, file) != envelope.signature_len) {
        fprintf(stderr, "Failed to read signature\n");
        fclose(file);
        goto cleanup;
    }

    fclose(file);

    printf("Digital envelope loaded:\n");
    printf("  Ciphertext length: %zu bytes\n", envelope.ciphertext_len);
    printf("  Encrypted key length: %zu bytes\n", envelope.encrypted_key_len);
    printf("  Signature length: %zu bytes\n", envelope.signature_len);

    // 提取数字信封
    printf("\nExtracting digital envelope...\n");
    if (extract_digital_envelope(&envelope, bob_priv, alice_pub, &plaintext, &plaintext_len) != 1) {
        fprintf(stderr, "Failed to extract digital envelope\n");
        goto cleanup;
    }

    printf("\n✓ Digital envelope extracted successfully!\n");
    printf("Decrypted plaintext (%zu bytes): %.*s\n", plaintext_len, (int)plaintext_len, plaintext);

    // 保存解密后的明文
    file = fopen("decrypted.txt", "wb");
    if (file) {
        fwrite(plaintext, 1, plaintext_len, file);
        fclose(file);
        printf("Decrypted content saved to: decrypted.txt\n");
    }

    ret = 1;

cleanup:
    if (plaintext) free(plaintext);
    free_digital_envelope(&envelope);
    if (bob_priv) EVP_PKEY_free(bob_priv);
    if (alice_pub) EVP_PKEY_free(alice_pub);
    
    return ret ? 0 : -1;
}
